package com.jay.tools.utils.file_util;

import android.content.Intent;
import android.net.Uri;

import java.io.File;

/**
 * author: tanjianpei
 * date: 2018/8/3 003 10:31
 * <p>
 * description: 根据文件类型，调用外部程序打开文件
 */
public class FileIntentUtil {

    public static Intent getIntent(String param) {
        param = param.toLowerCase();

        Intent intent = null;
        if (param.endsWith(".doc") || param.endsWith(".docx")) {
            intent = getWordFileIntent(param);
        } else if (param.endsWith(".ppt") || param.endsWith(".pptx")) {
            intent = getPPTFileIntent(param);
        } else if (param.endsWith(".xls") || param.endsWith(".xlsx")) {
            intent = getExcelFileIntent(param);
        } else if (param.endsWith(".pdf")) {
            intent = getPdfFileIntent(param);
        } else if (param.endsWith(".txt")) {
            intent = getTextFileIntent(param, false);
        } else if (param.endsWith(".png") || param.endsWith(".bmp")
                || param.endsWith(".jpg") || param.endsWith(".gif")
                || param.endsWith(".jpeg")) {
            intent = getImageFileIntent(param);
        } else if (param.endsWith(".htm") || param.endsWith(".html")
                || param.endsWith(".asp") || param.endsWith(".jsp")
                || param.endsWith(".php")) {
            intent = getHtmlFileIntent(param);
        }
        return intent;
    }

    // PPT文件
    private static Intent getPPTFileIntent(String param) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(new File(param)), "application/vnd.ms-powerpoint");
        return intent;
    }

    // Excel文件
    private static Intent getExcelFileIntent(String param) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(new File(param)), "application/vnd.ms-excel");
        return intent;
    }

    // Word文件
    private static Intent getWordFileIntent(String param) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(new File(param)), "application/msword");
        return intent;
    }

    // 文本文件
    private static Intent getTextFileIntent(String param, boolean paramBoolean) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (paramBoolean) {
            intent.setDataAndType(Uri.parse(param), "text/plain");
        } else {
            intent.setDataAndType(Uri.fromFile(new File(param)), "text/plain");
        }
        return intent;
    }

    // PDF文件
    private static Intent getPdfFileIntent(String param) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(new File(param)), "application/pdf");
        return intent;
    }

    // 图片文件

    private static Intent getImageFileIntent(String param) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(new File(param)), "image/*");
        return intent;
    }

    // HTML文件
    private static Intent getHtmlFileIntent(String param) {
        Intent intent = new Intent("android.intent.action.VIEW");
        Uri uri = Uri.parse(param).buildUpon()
                .encodedAuthority("com.android.htmlfileprovider")
                .scheme("content").encodedPath(param).build();
        intent.setDataAndType(uri, "text/html");
        return intent;
    }


}
